from pydantic_settings import BaseSettings
from typing import Optional
import os


class Settings(BaseSettings):
    # 应用基本配置
    app_name: str = "Gitee Webhook自动部署服务"
    environment: str = "development"
    host: str = "0.0.0.0" 
    port: int = 8000

    # 管理员配置
    admin_username: str
    admin_password: str

    # Gitee Webhook配置
    webhook_secret: str = "your_gitee_token"
    deploy_script: str = "/var/www/auto_deploy/scripts/deploy.sh"

    # 钉钉机器人配置
    dingtalk_access_token: str 
    dingtalk_secret: str

    # 日志配置
    log_level: str = "INFO"
    log_dir: str = "webhook_logs"

    # 数据库配置 (新增)
    # 对于 SQLite，database_url 通常是 'sqlite:///./sql_app.db'
    # 表示在当前目录下创建一个名为 'sql_app.db' 的 SQLite 数据库文件
    database_url: str = "sqlite:///./webhook_deploy.db"

    class Config:
        env_file = ".env"
        case_sensitive = False
        extra = "allow"  # 允许额外字段

    def get_webhook_log_path(self) -> str:
        """获取webhook日志保存路径"""
        if not os.path.exists(self.log_dir):
            os.makedirs(self.log_dir)
        return self.log_dir

settings = Settings()
